package com.example.guiguiapplicatio.fragment.SigningArchives.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.Signing;
import com.example.guiguiapplicatio.fragment.home.adapter.ServicepacksListAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SigningArchivesAdapter extends RecyclerView.Adapter<SigningArchivesAdapter.SigningArchivesViewHolder>{
    private List<Signing> SigningArchives = new ArrayList<>();
    private Context mContext;

    public SigningArchivesAdapter(List<Signing> signingArchives,Context context) {
        SigningArchives = signingArchives;
        mContext = context;
    }

    @NonNull
    @Override
    public SigningArchivesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_total_detail_report_item, parent, false);
        return new SigningArchivesAdapter.SigningArchivesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SigningArchivesViewHolder holder, int position) {
        Signing signing = SigningArchives.get(position);
        if(signing.getType().equals("1")){
            holder.text_sequence_number.setText("家庭签约记录");
        }
        if (signing.getType().equals("0")){
            holder.text_sequence_number.setText("个人签约记录");
        }
        if (signing.getEndtime() != null && signing.getEndtime().before(new Date())) {
            holder.text_status.setText("已过期");
        }
        if (signing.getEndtime() != null && signing.getEndtime().after(new Date()) && SigningArchives.size() != 1 ) {
            holder.text_status.setText("已续约");
        }
        if (signing.getEndtime() != null && signing.getEndtime().after(new Date()) && SigningArchives.size() == 1 ) {
            holder.text_status.setText("已签约");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String staTimeString = dateFormat.format(signing.getStarttime());
        holder.text_time.setText(staTimeString);

        holder.itemView.clearAnimation();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signing.getType().equals("1")){
                    Bundle bundle = new Bundle();
                    bundle.putString("idCard", signing.getIdcard());
                    bundle.putString("signingId", String.valueOf(signing.getId()));
                    // 使用 NavController 导航到目标 fragment，并将 Bundle 作为参数传递
                    Navigation.findNavController(v).navigate(R.id.action_SigningArchivesFragment_to_SigningdetailFragment, bundle);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return SigningArchives.size();
    }

    public static class  SigningArchivesViewHolder extends RecyclerView.ViewHolder {
        TextView text_sequence_number;
        TextView text_status;
        TextView text_time;

        public  SigningArchivesViewHolder(@NonNull View itemView) {
            super(itemView);
            text_sequence_number = itemView.findViewById(R.id.text_sequence_number);
            text_status = itemView.findViewById(R.id.text_status);
            text_time = itemView.findViewById(R.id.text_time);
        }
    }
}
